package com.blank.client;

import java.awt.AWTException;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;

import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import com.melloware.jintellitype.HotkeyListener;
import com.melloware.jintellitype.JIntellitype;

public class MainFrame extends JFrame {

	private static final long serialVersionUID = 1L;

	private JRadioButton model1 = new JRadioButton("编辑模式1");
	private JRadioButton model2 = new JRadioButton("编辑模式2");
	private JComboBox<String> jcb = new JComboBox<>();
	private ButtonGroup btg = new ButtonGroup();
	private Properties p = new Properties();
	private ScreenShotClient jframe = null;
	private JPanel jp = new JPanel();
	private String hotKey;

	public void Init() {
		
		
	    MainFrame.this.setTitle("screenshot");

		jcb.addItem("A");
		jcb.addItem("B");
		jcb.addItem("C");
		jcb.addItem("D");
		jcb.addItem("E");
		jcb.addItem("F");
		jcb.addItem("G");
		jcb.addItem("H");
		jcb.addItem("I");
		jcb.addItem("J");
		jcb.addItem("K");
		jcb.addItem("L");
		jcb.addItem("M");
		jcb.addItem("N");
		jcb.addItem("O");
		jcb.addItem("P");
		jcb.addItem("Q");
		jcb.addItem("R");
		jcb.addItem("S");
		jcb.addItem("T");
		jcb.addItem("U");
		jcb.addItem("V");
		jcb.addItem("W");
		jcb.addItem("X");
		jcb.addItem("Y");
		jcb.addItem("Z");

		this.add(jp);
		jp.add(new JLabel("快捷键: ALT + SHIFT + "));
		jp.add(jcb);

		try {

			p.load(new FileInputStream("config.properties"));
		} catch (IOException e2) {
			e2.printStackTrace();
		}

		hotKey = p.getProperty("hotKey");
		jcb.setSelectedItem(hotKey);

		this.setSize(300, 200);
		this.setLocationRelativeTo(null);
		SystemTray tray = SystemTray.getSystemTray();

		// 创建托盘图标：1.显示图标Image 2.停留提示text 3.弹出菜单popupMenu 4.创建托盘图标实例
		// 1.创建Image图像
		Image image = GenerateImage("");
		// 2.停留提示text
		String text = "here";
		// 3.弹出菜单popupMenu
		PopupMenu popMenu = new PopupMenu();
		MenuItem itmOpen = new MenuItem("open");
		itmOpen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MainFrame.this.setVisible(true);
			}
		});
		MenuItem itmHide = new MenuItem("hide");
		itmHide.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MainFrame.this.setVisible(false);
			}
		});
		MenuItem itmExit = new MenuItem("exit");
		itmExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		popMenu.add(itmOpen);
		popMenu.add(itmHide);
		popMenu.add(itmExit);
		
		
		
		TrayIcon trayIcon = new TrayIcon(image, text, popMenu);
		// 将托盘图标加到托盘上
		try {
			tray.add(trayIcon);
		} catch (AWTException e1) {
			e1.printStackTrace();
		}
		
		trayIcon.addMouseListener(new MouseAdapter() {
			
			public void mouseClicked(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON1) {
					MainFrame.this.setVisible(true);
				}
			}
			
		});

		jcb.addItemListener(new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent arg0) {
				if (arg0.getStateChange() == ItemEvent.SELECTED) {

					hotKey = arg0.getItem().toString();

					p.setProperty("hotKey", hotKey);

					try {
						p.store(new PrintWriter("config.properties"), "config.properties");
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					JIntellitype.getInstance().unregisterHotKey(1);
					InitHotKey();

				}

			}
		});

		btg.add(model1);
		btg.add(model2);
		this.setLayout(new GridLayout(3, 1));
		this.add(model1);
		this.add(model2);
		if (p.get("model").equals("1"))
			model1.setSelected(true);
		else
			model2.setSelected(true);

		model1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				p.setProperty("model", "1");
				try {
					p.store(new PrintWriter("config.properties"), "config.properties");
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});

		model2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				p.setProperty("model", "2");
				try {
					p.store(new PrintWriter("config.properties"), "config.properties");
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});

	}

	public void InitHotKey() {

		JIntellitype.getInstance().registerHotKey(1, JIntellitype.MOD_CONTROL + JIntellitype.MOD_SHIFT,
				hotKey.toCharArray()[0]);

		JIntellitype.getInstance().addHotKeyListener(new HotkeyListener() {

			@Override
			public void onHotKey(int markCode) {
				switch (markCode) {
				case 1:
					try {
						MainFrame.this.setVisible(false);
						Runtime.getRuntime().exec("nircmd savescreenshot a.png").waitFor();
						p.load(new FileInputStream("config.properties"));
						if (p.get("model").equals("1")) {
							if (jframe == null) {
								jframe = new ScreenShotClient();
								jframe.setUndecorated(true);
							}
							jframe.updateImg();
							jframe.setVisible(true);

						} else {
							Runtime.getRuntime().exec("mspaint a.png");
						}
					} catch (IOException | InterruptedException e) {
						e.printStackTrace();
					}

					break;
				}
			}
		});

	}

	public MainFrame() {

		Init();
		InitHotKey();
		this.setVisible(true);
	}

	public Image GenerateImage(String imgStr) {

		try {
			return ImageIO.read(new File("blue_cut_16px_569328_easyicon.net.png"));
		} catch (IOException e1) {
			e1.printStackTrace();
			return null;
		}

	}

	public static void main(String[] args) throws IOException {

		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			new MainFrame();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
